﻿/**
*
*
*
*
*  
*   
*	@class:		Segment
*	@author:		andy hatch
*	@version:		1.0
*	@date:		2008-03-26
*/ 

import mx.transitions.easing.Elastic;
import gs.TweenLite;
//import gs.TweenFilterLite;
import mx.utils.Delegate;
import flash.filters.BlurFilter;


class com.hatch.effects.odometer.Segment extends MovieClip
{
	// ON STAGE
	var mask:MovieClip;
	
	
	// CLASS PROPERTIES
	var _alphabet:Array;
	var _currentVal:Number;
	var _currentChar:MovieClip;
	var _nextChar:MovieClip;
	var _scrollTo:String;
	var _tweenTime:Number;
	var _me;
	

	// DELEGATES


	// EVENT DISPATCHERS


	
	/* *********************************************************
	*  CONSTRUCTOR
	*  *********************************************************/

	
	function Segment()
	{	
		//initialize();
	}


	private function initialize():Void
	{
		//trace("Segment :: initialize()")
		//define alphabet
		_alphabet = new Array(' ', "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
		_currentVal = 1;
		_currentChar = newChar(_alphabet[_currentVal]);
		_nextChar = newChar(_alphabet[_currentVal]);
		_tweenTime = .04;
		this.setMask(mask);
		
		_me = this;
	} 



	/* *********************************************************
	*  PUBLIC METHODS
	*  *********************************************************/
	public function scrollToCharacter(character:String):Void
	{
		//trace("Segment :: scrollToCharacter() :: character = " + character);
		if(character == undefined)
		{
			_scrollTo = " ";
		}
		else
		{
			_scrollTo = character;
		}
		
		if(_alphabet[_currentVal] == _scrollTo)
		{
			onScrollComplete();
		}
		else
		{
			characterScroll();
		}
		
	}

	/* *********************************************************
	*  PRIVATE METHODS
	*  *********************************************************/
	private function newChar(value:String):MovieClip
	{
		//trace("Segment :: newChar() :: value = " + value);
		var newChar = attachMovie("dynamicCharacter", "char", getNextHighestDepth());
		newChar.setValue(String(value));
		
		return newChar;
	}
	
	
	private function characterScroll():Void
	{
		//trace("Segment :: characterScroll() _scrollTo = " + _scrollTo);
		if(_scrollTo == _alphabet[_currentVal])
		{
			onScrollComplete();
			return;
		}
		else
		{
			_nextChar.setValue(_alphabet[getNextNumber(_currentVal)]);
			
		}
		
		//evaluate if the next tween is the target character
		if(_scrollTo == _alphabet[getNextNumber(_currentVal)])
		{
			tweenSettle();
		}
		else
		{
			tweenThrough();
		}
	}
	
	
	private function killScroll():Void
	{
		
	}

	
	private function evalCharacter():Void
	{
		
	}
	
	
	private function tweenThrough():Void
	{
		//trace("Segment :: tweenThrough()")
		//2 tweens:
		//tween current displayed mc down to mc._height :: onComplete bump this back to top(-mc._height)
		//tween new mc down to 0
		var bottom_mc:MovieClip = _currentChar;
		var top_mc:MovieClip = _nextChar;
		
		bottom_mc._y = 0;
		top_mc._y = -top_mc._height;
		
		setFilters(bottom_mc);
		setFilters(top_mc);
		
		TweenLite.to(bottom_mc, _tweenTime, {_y: this._height});
	//	TweenLite.to(top_mc, _tweenTime, {_y: 0, onComplete: onTweenComplete, onCompleteParams: [bottom_mc, top_mc, _currentVal, characterScroll], overwrite: false});
		TweenLite.to(top_mc, _tweenTime, {_y: 0, onComplete: Delegate.create(_me, onTweenComplete), overwrite: false});
		
		/*
		function onTweenComplete()
		{
			bottom_mc._y = -bottom_mc._height;
			_nextChar = bottom_mc;
			_currentChar = top_mc;

			_currentVal++;
			trace(characterScroll);
			characterScroll();
		}
		*/
	}
	
	private function setFilters(mc:MovieClip, clearFilter:Boolean):Void
	{
		if(!clearFilter)
		{
			var blur:BlurFilter = new BlurFilter(5, 5, 3);
			var filterArray:Array = new Array();
			filterArray.push(blur);
			mc.filters = filterArray;
		}
		else if(clearFilter)
		{
			mc.filters = null;
		}
	}
	
	
	private function tweenSettle():Void
	{
		//tweens the character to settle (elastic ease)
		var bottom_mc:MovieClip = _currentChar;
		var top_mc:MovieClip = _nextChar;
		
		setFilters(bottom_mc, true);
		setFilters(top_mc, true);
		
		TweenLite.to(bottom_mc, _tweenTime, {_y: this._height});
		TweenLite.to(top_mc, _tweenTime+.5, {_y: 0, onComplete: Delegate.create(_me, onTweenComplete), overwrite: false, ease: Elastic.easeOut});
		//TweenFilterLite.from(top_mc, .5, {colorMatrixFilter:{brightness:2}});
		/*
		function onTweenComplete()
		{
			bottom_mc._y = -bottom_mc._height;
			_nextChar = bottom_mc;
			_currentChar = top_mc;

			_currentVal++;

			characterScroll();
		}
		*/
	}
	
	
	private function onTweenComplete():Void
	{
		//trace("Segment :: onTweenComplete()");
	
		var bottom_mc:MovieClip = _currentChar;
		var top_mc:MovieClip = _nextChar;
		
		bottom_mc._y = -bottom_mc._height;
		
		_nextChar = bottom_mc;
		_currentChar = top_mc;
		
		_currentVal = getNextNumber(_currentVal);
		
		characterScroll();
	}
	

	private function getNextNumber(n:Number):Number
	{
		return (n == _alphabet.length) ? 0 : ++n;
	}



	/* *********************************************************
	*  EVENT HANDLERS
	*  *********************************************************/
	
	private function onScrollComplete():Void
	{
		//trace("Segemnt :: onScrollComplete()");
		//dispatch an event to let Odometer know the character has been reached
	}





	/* *********************************************************
	*  GETTERS/SETTERS
	*  *********************************************************/    

};